package com.fcl.oschina.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.fcl.mylibrary.recyclerviewhelper.BaseAdapter;
import com.fcl.mylibrary.util.FLog;
import com.fcl.oschina.R;
import com.fcl.oschina.adapter.BlogAdapter;
import com.fcl.oschina.entity.NewItem;
import com.fcl.oschina.model.BlogModel;
import com.fcl.oschina.util.ToastUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/3/20.
 */

public class BlogFragment extends BaseRecyclerFragment<NewItem> implements BlogView {

    /**
     * 博客类型
     */
    public static final int CATALOG_BLOG_NORMAL = 1; // 最新
    public static final int CATALOG_BLOG_HEAT = 2; // 最热
    public static final int CATALOG_BLOG_RECOMMEND = 3;//推荐

    public static final int COMMON = 0; //普通博客
    public static final int USER = 1; //用户博客
    
    private String TAG = "BlogFragment";
    private BlogModel mBlogModel;
    private BlogAdapter mBlogAdapter;
    private int mCatalog = CATALOG_BLOG_HEAT;
    private int mType = COMMON;
    private int mAuthorId;

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getArguments();
        if (bundle != null) {
            mType = bundle.getInt("type");
            mAuthorId = bundle.getInt("author_id");
        }

        mBlogModel = new BlogModel(this);
        mBlogModel.setNextPage("");

        getData();
        mBlogAdapter = (BlogAdapter) mAdapter;
        mBlogAdapter.setItemClickListener(new BaseAdapter.MyClickListener() {
            @Override
            public void onClick(int pos) {
//                ToastUtil.showDefineToast("点击位置:"+pos);
                NewItem newItem = mDatas.get(pos);
                jumpActivity(newItem.getType(), newItem.getId());
            }
        });
    }

    private void getData() {
        switch (mType) {
            case COMMON:
                mBlogModel.getBlogList(mCatalog);
                break;
            case USER:
                mBlogModel.getUserBlogList(mAuthorId);
                break;
        }
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        return new BlogAdapter(getContext(), R.layout.item_news,  mDatas);
    }

    @Override
    protected void onRefresh() {
        super.onRefresh();
        mSceneLayout.setLoading();
        mBlogModel.setNextPage("");
        getData();
    }

    @Override
    public void setData(final List<NewItem> data, final boolean add) {
        FLog.error(TAG, " thread" + Thread.currentThread().getId());
        if (add) {
            mDatas.addAll(data);
        } else {
            mDatas = data;
        }
        mBlogAdapter.setDatas(mDatas);
        mStickyLayout.finishRefresh();
        mSceneLayout.setNormal();
    }

    @Override
    protected void loadMore(int curPage) {
        super.loadMore(curPage);
        getData();
    }
}
